package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.entity.*;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.service.SpuSaleAttrValueService;
import com.atguigu.gmall.product.vo.SpuSaveVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.mapper.SpuInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author DELL
* @description 针对表【spu_info(商品表)】的数据库操作Service实现
* @createDate 2022-11-29 22:54:36
*/
@Slf4j
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo>
    implements SpuInfoService{
    @Autowired
    private SpuImageService spuImageService;
    @Autowired
    private SpuSaleAttrService spuSaleAttrService ;
    @Autowired
    private SpuSaleAttrValueService  spuSaleAttrValueService;
    @Override
    public void saveSpuInfo(SpuSaveVo spuSaveVo) {
        log.info("保存 spu {}",spuSaveVo);
        //将数据封装在 spuInfo中
        SpuInfo spuInfo =new SpuInfo();
        BeanUtils.copyProperties(spuSaveVo,spuInfo);
        save(spuInfo);   // 将拿到的数据 保存道 spuinfo表中
        Long spuId = spuInfo.getId();
        //1 保存图片
        List<SpuImage> collect = spuSaveVo.getSpuImageList().stream()
                .map(item -> {
                    SpuImage spuImage = new SpuImage();
                    BeanUtils.copyProperties(item, spuImage);
                    spuImage.setSpuId(spuId);
                    return spuImage;
                }).collect(Collectors.toList());
        spuImageService.saveBatch(collect);
        //2 保存 销售属性名
        List<SpuSaleAttr> spuSaleAttrs = spuSaveVo.getSpuSaleAttrList()
                .stream()
                .map(item -> {
                    SpuSaleAttr spuSaleAttr = new SpuSaleAttr();
                    BeanUtils.copyProperties(item, spuSaleAttr);
                    spuSaleAttr.setSpuId(spuId);
                    return spuSaleAttr;
                }).collect(Collectors.toList());
          spuSaleAttrService.saveBatch(spuSaleAttrs);
          //3 存属性值 "spuSaleAttrValueList" spu_sale_attr_value
        List<SpuSaleAttrValue> values = spuSaveVo.getSpuSaleAttrList()
                .stream()
                //item是每个销售属性，带了很多销售属性值
                .flatMap(item -> {
                    return item.getSpuSaleAttrValueList()
                            .stream()
                            .map(val -> {
                                SpuSaleAttrValue spuSaleAttrValue = new SpuSaleAttrValue();
                                BeanUtils.copyProperties(val, spuSaleAttrValue);
                                spuSaleAttrValue.setSpuId(spuId);
                                spuSaleAttrValue.setBaseSaleAttrId(val.getBaseSaleAttrId());
                                spuSaleAttrValue.setSaleAttrValueName(val.getSaleAttrValueName());
                                spuSaleAttrValue.setSaleAttrName(item.getSaleAttrName());
                                return spuSaleAttrValue;
                            });
                }).collect(Collectors.toList());
            spuSaleAttrValueService.saveBatch(values);
    }

}


